﻿using EasyCore.Excel;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Excel
{
    public class ExcelMapper
    {
        public bool IsXlxs { get; set; }

        /// <summary>
        /// 工作簿
        /// </summary>
        public IWorkbook Workbook { get; set; }

        /// <summary>
        /// 工作簿
        /// </summary>
        public List<ISheetMapper> SheetMappers { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xlsx"></param>
        public ExcelMapper(bool isXlsx = true)
        {
            this.IsXlxs = isXlsx;
            if (this.IsXlxs) Workbook = new XSSFWorkbook(); //07版以上          
            else Workbook = new HSSFWorkbook(); //07版以下  
        }

        /// <summary>
        /// 创建datatable映射器
        /// </summary>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public DataTableSheetMapper CreateDataTableSheetMapper(string sheetName)
        {
            var mapper = new DataTableSheetMapper(this, sheetName); //创建工作簿映射器
            if (SheetMappers == null) SheetMappers = new List<ISheetMapper>();
            SheetMappers.Add(mapper);
            return mapper;
        }

        /// <summary>
        /// 获得实体映射器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public EntitySheetMapper<T> CreateEntitySheetMapper<T>(string sheetName)
        {
            var mapper = new EntitySheetMapper<T>(this, sheetName); //创建工作簿映射器
            if (SheetMappers == null) SheetMappers = new List<ISheetMapper>();
            SheetMappers.Add(mapper);
            return mapper;
        }

        /// <summary>
        /// 保存到文件夹中
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public void Save(string filePath)
        {
            if(File.Exists(filePath)) File.Delete(filePath);
            using (FileStream file = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write)) {
                foreach (var sheetMapper in this.SheetMappers)
                {
                    sheetMapper.Write();
                }
                Workbook.Write(file);
                file.Dispose();
            } 
        }

        /// <summary>
        /// 导出到内存流中
        /// </summary>
        /// <returns></returns>
        public MemoryStream Export()
        {
            var stream = new ExcelMemoryStream(false);
            foreach (var sheetMapper in this.SheetMappers)
            {            
                 sheetMapper.Write();
            }
            Workbook.Write(stream, true);
            stream.Flush();
            stream.Position = 0;
            stream.AllowClose = true;
            return stream;
        }
    }
}
